package com.homedo.odin.solution.service.bizservice.pc;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.BeanUtils;
import com.homedo.odin.solution.contract.pc.UserCommentAddPcRequestBean;
import com.homedo.odin.solution.contract.pc.UserCommentListPCResponseBean;
import com.homedo.odin.solution.contract.pc.UserCommentPcResponseBean;
import com.homedo.odin.solution.contract.personal.treasure.AddScoreReq;
import com.homedo.odin.solution.contract.personal.userhomepage.AddPopularityReq;
import com.homedo.odin.solution.enumcode.TemplateCodeEnum;
import com.homedo.odin.solution.enumcode.basic.ColumnTypeEnum;
import com.homedo.odin.solution.enumcode.comment.UserCommentStatus;
import com.homedo.odin.solution.enumcode.user.BizCodeEnum;
import com.homedo.odin.solution.enumcode.user.ReplyStatusEnum;
import com.homedo.odin.solution.enumcode.user.VisitTypeEnum;
import com.homedo.odin.solution.model.dto.PageBean;
import com.homedo.odin.solution.model.entity.Solution;
import com.homedo.odin.solution.model.entity.SpecialColumn;
import com.homedo.odin.solution.model.entity.Specification;
import com.homedo.odin.solution.model.entity.UserComment;
import com.homedo.odin.solution.service.SolutionServiceImpl;
import com.homedo.odin.solution.service.SpecialColumnServiceImpl;
import com.homedo.odin.solution.service.SpecificationServiceImpl;
import com.homedo.odin.solution.service.UserCommentServiceImpl;
import com.homedo.odin.solution.service.bizservice.personal.CommonBizService;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author: ZhangLei
 * @date: 2018/4/9
 * @Description:
 */
@Service
public class UserCommentService {

    @Autowired
    private UserCommentServiceImpl userCommentService;

    @Autowired
    private SpecialColumnServiceImpl specialColumnService;

    @Autowired
    private SolutionServiceImpl solutionService;

    @Autowired
    private SpecificationServiceImpl specificationService;

    @Autowired
    private CommonBizService commonBizService;


    /**
     * 添加评论
     */
    @Transactional(rollbackFor = Exception.class)
    public Long pcAddUserComment(UserCommentAddPcRequestBean requestBean) throws Exception {

        if (requestBean.getType().intValue() == ColumnTypeEnum.SPECIFICATION.getDbValue()) {
            Specification specification = specificationService.getSpecification(requestBean.getColumnId());
            if (specification == null) {
                throw new BizException(Meta.CODE_451.getCode(), "该行业规范不存在");
            }
        } else if (requestBean.getType().intValue() == ColumnTypeEnum.SOLUTION.getDbValue()) {
            Solution solution = solutionService.getSolution(requestBean.getColumnId());
            if (solution == null) {
                throw new BizException(Meta.CODE_451.getCode(), "该方案不存在");
            }

            //评论计数
            solutionService.addSolutionComment(solution.getSolutionId());

            //发送通知
            Map<String, String> paramMap = Maps.newHashMap();
            paramMap.put("@@userName", solution.getUserName());
            paramMap.put("@@bizId", String.valueOf(solution.getSolutionId()));
            paramMap.put("@@solution", solution.getSolutionName());
            commonBizService
                    .sentMessage(paramMap, solution.getUserId(), 0, TemplateCodeEnum.solution_comment.toString());

            //增加人气
            AddPopularityReq req = new AddPopularityReq();
            req.setOperationId(requestBean.getUserId());
            req.setTargetId(solution.getUserId());
            req.setType(VisitTypeEnum.COMMENT.getDbValue());
            commonBizService.addPopularity(req);

        } else {
            SpecialColumn specialColumn = specialColumnService.getSpecialColumn(requestBean.getColumnId());
            if (specialColumn == null) {
                throw new BizException(Meta.CODE_451.getCode(), "该栏目不存在");
            }
        }

        UserComment userComment = BeanUtils.transfrom(UserComment.class, requestBean);
        userComment.setReplyStatus(ReplyStatusEnum.NOT_REPLY.getDbValue());
        userComment.setAuditStatus(UserCommentStatus.PENDINGAUDIT.getDbValue());
        Long commentId = userCommentService.addUserComment(userComment);

        //增加积分
        AddScoreReq req = new AddScoreReq();
        req.setUserId(requestBean.getUserId());
        req.setType(BizCodeEnum.COMMENT.getDbValue());
        req.setBizCode(String.valueOf(commentId));
        commonBizService.addScore(req);

        /*//增加该栏目的评论量
		columnService.addSpecialColumnCommentCount(userCommentAddRequest.getColumnId());*/
        return commentId;

    }

    /**
     * 评论信息列表
     */
    public UserCommentListPCResponseBean queryUserCommentListByColumnId(Long columnId, Integer userId,
            Integer currentPage, Integer pageSize) {
        PageBean pageBean = new PageBean(currentPage, pageSize);
        PageInfo<UserComment> pageResult = userCommentService
                .queryUserCommentListByColumnId(columnId, userId, pageBean);
        List<UserComment> list = pageResult.getList();
        List<UserCommentPcResponseBean> resultList = new ArrayList<>();

        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(userComment -> {
                UserCommentPcResponseBean userCommentResponse = BeanUtils
                        .transfrom(UserCommentPcResponseBean.class, userComment);
                resultList.add(userCommentResponse);
            });
        }

        UserCommentListPCResponseBean responseBean = BeanUtils
                .transfrom(UserCommentListPCResponseBean.class, pageResult);
        responseBean.setList(resultList);
        return responseBean;
    }


    /**
     * 点赞评论
     */
    public void likeUserComment(Long commentId, Integer userId) throws Exception {
        UserComment userComment = userCommentService.selectOne(commentId);
        if (userComment == null) {
            throw new BizException(Meta.CODE_451.getCode(), "该评论不存在");
        }

        //点赞数+1
        userCommentService.addUserCommentLike(commentId);

        //增加积分
        AddScoreReq req = new AddScoreReq();
        req.setUserId(userId);
        req.setType(3);
        req.setBizCode(commentId.toString());
        commonBizService.addScore(req);

        //增加人气
        AddPopularityReq popularity = new AddPopularityReq();
        popularity.setOperationId(userId);
        popularity.setTargetId(userComment.getUserId());
        popularity.setType(2);
        commonBizService.addPopularity(popularity);
    }
}
